package com.zk.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zk.entity.AllMenu;
import com.zk.entity.AllProducts;
import com.zk.entity.ResultDto;
import com.zk.mapper.AllMenuMapper;
import com.zk.mapper.AllProductsMapper;
import com.zk.service.IAllMenuService;
import com.zk.service.IAllProductsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ljw
 * @since 2020-11-29
 */
@RestController
@RequestMapping("/zk/all-products")
@Api(value = "产品信息管理")
public class AllProductsController {
    @Autowired
    private IAllProductsService iAllProductsService;
    @Resource
    private AllProductsMapper allProductsMapper;

    /**
     * 增加产品
     */
    @RequestMapping(value = "/",method = RequestMethod.POST)
    @ApiOperation(value="增加产品", notes="test: 仅1和2有正确返回")
    public ResultDto insertAllProducts(AllProducts allProducts){
        boolean count = iAllProductsService.save(allProducts);
        return new ResultDto(200,"插入成功",count);
    }
    /**
     * 根据id删除产品
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    @ApiOperation(value="删除产品", notes="test: 仅1和2有正确返回")
    public void deleteAllProducts(@PathVariable("id") Serializable id){
        System.out.println(id);
        iAllProductsService.removeById(id);
    }

    /**
     * 根据id修改产品
     */
    @RequestMapping(value = "/",method = RequestMethod.PUT)
    @ApiOperation(value="根据id修改产品", notes="test: 仅1和2有正确返回")
    public void  updateById(AllProducts allProducts){
        iAllProductsService.updateById(allProducts);
    }
    /**
     * 根据id查询产品
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    @ApiOperation(value="查询id产品信息", notes="test: 仅1和2有正确返回")
    public AllProducts  selectById(@RequestParam("id") int id){
        QueryWrapper<AllProducts> allProductsQueryWrapper = new QueryWrapper<>();
        allProductsQueryWrapper.eq("product_id",id);
        AllProducts allProducts = iAllProductsService.getOne(allProductsQueryWrapper);
        return  allProducts;
    }

    /**
     * 查询所有产品
     */

    @RequestMapping(value = "/",method = RequestMethod.GET)
    @ApiOperation(value="查询所有产品信息", notes="test: 仅1和2有正确返回")
    public List<AllProducts> listall(){
        List<AllProducts> allProducts = iAllProductsService.list(new QueryWrapper<>());
        return allProducts;
    }

    /**
     * 分页查询菜单
     */
    @RequestMapping(value = "/page",method = RequestMethod.GET)
    @ApiOperation(value="分页查询所有菜单信息", notes="test: 仅1和2有正确返回")
    public IPage<Map<String, Object>> pageInfo(){
        IPage<Map<String, Object>> page2=new Page<Map<String, Object>>(1,2);
        return allProductsMapper.selectMapsPage(page2 ,null);

    }
}

